<?php

namespace app\dao\setting;

use app\dao\BaseDao;
use app\exceptions\AuthException;
use app\model\setting\admin\SystemGroupAccess;
use app\model\setting\admin\SystemUser;
use app\model\setting\SystemMenus;

class SystemMenusDao extends BaseDao
{


    public function updateDao(array $where)
    {
        if ($where['menu'] == 1){
            $this->getModel()->where('pid', $where['id'])->where('menu', 0)->delete();
            if(!empty($where['front'])){
                $front = explode(',', $where['front']);
                foreach ($front as $val){
                    $this->getModel()::create(['pid' => $where['id'], 'pids' => $where['pids'] == null ? '' : $where['pids'] . ',' . $where['id'], 'name' => $val, 'title' => $val, 'hidden' => 1, 'routePath' => 0, 'componentsPath' => 0, 'front' => $val, 'weight' => 1, 'menu' => 0]);
                }
                unset($where['front']);
            }
          return  $this->getModel()->update($where);
        }else{
         return   $this -> getModel() -> update(['id' => $where['id'] , 'pids' => $where['pids'],'title' => $where['title'] ,'front' =>$where['permission']]);
        }
    }

    public function deleteDao($where)
    {

        if (is_array($where['id'])) {
            $data = $this->search()->select($where['id']);
            foreach ($data as $key => $val) {
                if ($val['menu'] === '菜单') {
                    $this->getModel()->where('pid', $val['id'])->where('menu', 0)->delete();
                }
                return $val->delete();
            }
        } else {
            $data = $this->search()->find($where['id']);
            if ($data['menu'] === '菜单') {//菜单查找所有节点删除
                $this->getModel()->where('pid', $data['id'])->where('menu', 0)->delete();
            }
            return $data->delete();
        }


    }

    public function searchTitle(array $where)
    {
        return $this->getModel()::withSearch(['title'], ['title' => $where['title'], 'sort' => ['weight' => 'asc']])->select();
    }

    public function searchPidAndNotMenu(array $where)
    {
        return $this ->getModel()::withSearch(['pid','menu'],[
            'pid' => $where['id'],
            'menu' => 0
        ]) -> select();
    }

    protected function setModel(): string
    {
        return SystemMenus::class;
    }

    public function getList($where,$userid)
    {
//        $where = array_merge($where, ['status' => 1]);
        $withoutField = ['update_time', 'website_id'];
        $userRole =  SystemUser::with(['roles']) ->  find($userid);
        $pid = $userRole['roles'][0]['pid'];
        if (!empty($pid)){
            $select = $userRole['roles'][0]['rules'].','.$userRole['roles'][0]['pid'];
        }else{
            $select =  $userRole['roles'][0]['rules'];
        }
        return $this->search($where)->withoutField($withoutField)->order('weight', 'asc') ->select(explode(',',$select));
    }

    /**
     * 这个不验证token
     * @param $where
     * @param $userid
     * @return array|\crmeb\basic\BaseModel[]|\think\Collection|\think\Model[]
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getMenusList($where)
    {
        $withoutField = ['update_time', 'website_id'];
        return $this->search($where)->withoutField($withoutField)->order('weight', 'asc')->select();
    }

    /**
     * 添加菜单
     * @return void
     */
    public function create($where)
    {
        $auth = $where['front'];
        if ($where['menu'] == 1) {//菜单
            unset($where['front']);
            $data = $this->getModel()::create($where);
            if ($data) {
                if (!empty($auth)) {
                    $front = explode(',', $auth);
                    foreach ($front as $v) {
                        $this->getModel()::create(['pid' => $data['id'], 'pids' => $data['pids'] == null ? '' : $data['pids'] . ',' . $data['id'], 'name' => $v, 'title' => $v, 'hidden' => 1, 'routePath' => 0, 'componentsPath' => 0, 'front' => $v, 'weight' => 1, 'menu' => 0,'website_id' => $where['website_id']]);
                    }
                }
                return $data;

            } else {
                throw new AuthException('添加数据失败！');
            }
        } else {
            return $this->getModel()::create(['pid' => $where['pid'], 'pids' => $where['pids'], 'name' => $where['name'], 'title' => $where['title'], 'hidden' => 1, 'routePath' => 0, 'componentsPath' => 0, 'front' => $where['permission'], 'weight' => 1, 'menu' => 0,'website_id' => $where['website_id']]);
        }

    }


}